"""
Verify personal agents pagination issue
"""
import sys
import os

# Add project root to path
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)

from src.llm.hiagent_app.services.personal_agent_service import PersonalAgentService
from src.llm.hiagent_app.models.hiagent_models import ListOption, ListAppBriefsFilter

def test_pagination():
    """Test pagination for personal agents"""
    print("\n=== Testing Personal Agents Pagination ===\n")

    service = PersonalAgentService()

    # Test 1: Page 1 with page_size=2
    print("Test 1: Page 1, PageSize=2")
    list_opt_1 = ListOption(page=1, page_size=2)
    agents_1, total_1 = service.list_personal_agents(list_opt=list_opt_1)
    print(f"Total: {total_1}")
    print(f"Returned items: {len(agents_1)}")
    for agent in agents_1:
        print(f"  - {agent.name} (ID: {agent.app_id})")

    # Test 2: Page 2 with page_size=2
    print("\nTest 2: Page 2, PageSize=2")
    list_opt_2 = ListOption(page=2, page_size=2)
    agents_2, total_2 = service.list_personal_agents(list_opt=list_opt_2)
    print(f"Total: {total_2}")
    print(f"Returned items: {len(agents_2)}")
    for agent in agents_2:
        print(f"  - {agent.name} (ID: {agent.app_id})")

    # Test 3: Page 1 with page_size=20
    print("\nTest 3: Page 1, PageSize=20")
    list_opt_3 = ListOption(page=1, page_size=20)
    agents_3, total_3 = service.list_personal_agents(list_opt=list_opt_3)
    print(f"Total: {total_3}")
    print(f"Returned items: {len(agents_3)}")

    # Compare results
    print("\n=== Pagination Check ===")
    if len(agents_1) == len(agents_3):
        print("❌ FAILED: Page 1 with different page_size returns same count!")
        print("   This indicates pagination is NOT working")
    else:
        print("✅ PASSED: Different page sizes return different results")

    # Check if page 2 returns different agents
    if len(agents_2) > 0:
        agent_1_ids = {a.app_id for a in agents_1}
        agent_2_ids = {a.app_id for a in agents_2}
        overlap = agent_1_ids & agent_2_ids
        if overlap:
            print(f"❌ FAILED: Page 1 and Page 2 have overlapping items: {overlap}")
        else:
            print("✅ PASSED: Page 1 and Page 2 have different items")
    else:
        print("⚠️  WARNING: Page 2 returned no items")

if __name__ == "__main__":
    test_pagination()
